package generation.aggregation;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import jm.JMC;
import jm.audio.Instrument;
import jm.audio.RTMixer;
import jm.music.rt.RTLine;

import com.illposed.osc.OSCListener;
import com.illposed.osc.OSCMessage;
import communication.OSCExternalInput;

public class InteractiveAggregator implements JMC, ChangeListener,
		ActionListener, OSCListener {
	private JSlider cutoff, panner;
	private RTMixer mixer;
	private JLabel val;
	private RTMelody melody1;
	private RTMelody melody2;
	private JButton goBtn;
	private RTLine[] lineArray = new RTLine[1];

	
	public static void main(String[] args) {
		new InteractiveAggregator();
	}

	public InteractiveAggregator() {
		int sampleRate = 44100;
		int channels = 2;

		Instrument[] instArray = new Instrument[1];

		// for (int i = 0; i < instArray.length; i++) {
		//instArray[0] = new SawLPFInstRT2(sampleRate, 1000, channels);
		 instArray[0] = new HarmonicsInst(sampleRate);
		// }

		melody1 = new RTMelody(instArray);
		melody1.setTempo(120);
		// melody2 = new MelodyLine(instArray);
		// melody2.setTempo(120);

		// bass2.setTempo(104);
		lineArray[0] = melody1;
		// lineArray[1] = melody2;
		// lineArray[1] = bass2;
		// lineArray[1] = bass2;
		// mixer = new RTMixer(lineArray);
		OSCExternalInput osc = new OSCExternalInput();
		osc.startListening(this);

		// show slider panel
		makeGUI();
	}

	private void makeGUI() {
		JFrame f = new JFrame("Title");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(100, 250);
		JPanel panel = new JPanel(new BorderLayout());
		f.getContentPane().add(panel);
		// pan
		panner = new JSlider(1, 1, 100, 50);
		panner.setEnabled(false);
		panner.addChangeListener(this);
		panel.add(panner, "West");
		// filter
		cutoff = new JSlider(1, 1, 100, 10);
		cutoff.setEnabled(false);
		cutoff.addChangeListener(this);
		panel.add(cutoff, "Center");
		// filter value display
		val = new JLabel("1000");
		panel.add(val, "East");
		// start stop
		goBtn = new JButton("Start");
		goBtn.addActionListener(this);
		panel.add(goBtn, "North");

		//
		f.pack();
		f.setVisible(true);
	}

	public void stateChanged(ChangeEvent e) {
		// panner slider moved
		if (e.getSource() == panner) {
			melody1.setValueX((double) panner.getValue() / 100.0);
			// melody2.setValueX((double) panner.getValue() / 100.0);
		}
		// filter slider moved
		if (e.getSource() == cutoff) {
			val.setText("" + (cutoff.getValue() * 100));
			melody1.setValueY(cutoff.getValue() / 100.0);
			// melody2.setValueY(cutoff.getValue() / 100.0);
			// mixer.actionLines(cutoff, 1);
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == goBtn) {
			mixer = new RTMixer(lineArray);
			cutoff.setEnabled(true);
			panner.setEnabled(true);
			goBtn.setEnabled(false);
			mixer.begin();
		}
	}

	@Override
	public void acceptMessage(Date date, OSCMessage message) {
		// System.out.println(message.getAddress());
		Object[] args = message.getArguments();
		Float x = (Float) args[0];
		Float y = (Float) args[1];
		melody1.setValueX(x);
		melody1.setValueY(y);
		// melody2.setValueX(x);
		// melody2.setValueY(y);
	}

}
